const express = require('express');
const router = express.Router();
const {Course, Category, User} = require('../models');
const {Success, failure} = require('../utils/responses');

router.get('/', async function (req, res, next) {
    try {
        const recommendedCourses = await Course.findAll({
          attributes:{exclude:['CategoryId','UserId','content']},
          include:[
            {
              model:Category,
              as:'category',
              attributes:['id','name'],
            },
            {
              model:User,
              as:'user',
              attributes:['id','username','nickname','avatar','company'],
            }
          ],
          where:{recommended:true},
          order:[['id','DESC']],
          limit:10
        });

        //人气课程
      const likesCourse = await Course.findAll({
        attributes:{exclude:['CategoryId','UserId','content']},
        order:[['id','DESC'],['id','DESC']],
        limit:10
      });

      //入门课程
      const introductoryCourses = await Course.findAll({
        attributes:{exclude:['CategoryId','UserId','content']},
        where:{introductory:true},
        order:[['id','DESC']],
        limit:10
      });

      Success(res,'获取首页数据成功。',{
        recommendedCourses,
        likesCourse,
        introductoryCourses
      });
    } catch (error) {
        failure(res, error);
    }
});
module.exports = router;